(function(angular) {
	'report strict';
	angular.module('fs.waf').controller('cda.report.createController', editController);
	editController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService','storageService',
			'reportService' ];
	function editController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, storageService, reportService) {
		var ctrl = this, reportInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.validatorConfig = validatorConfig;
		ctrl.init = function() {
			// init report Parameter
			reportService.initDataOptions($scope, ctrl);
			authService.create(reportInfo).then(function(response) {
				// init report Parameter
				reportService.initReportParameter($scope, ctrl, layoutConfig);
				// init report company
				reportService.initCompany($scope, ctrl, false, layoutConfig);
				ctrl.report = response.data || reportInfo;
				ctrl.report.reportUrl = "cda.reportView.defaultView";
				ctrl.report.siteCode = storageService.getUserInfo().siteCode;
				ctrl.report.reportParms = ctrl.report.reportParms || [];
				ctrl.report.companyRpts = ctrl.report.companyRpts || [];
				ctrl.layoutConfig = layoutConfig;
				ctrl.changeReportEngine();
			});
		};
		
		ctrl.changeServiceCode = function(){
			if ( ctrl.report && ctrl.report.reportName && ctrl.report.serviceCode){				
				ctrl.report.siteCode = storageService.getUserInfo().siteCode;
				ctrl.report.reportCode = ctrl.report.siteCode + ":" + ctrl.report.serviceCode + ":" + ctrl.report.reportName;
			} else {
				if ( ctrl.report){
					ctrl.report.reportCode = null;
				}
			}
		};
		
		ctrl.changeReportCode = function(){
			if ( ctrl.report && ctrl.report.reportName && ctrl.report.serviceCode){				
				ctrl.report.siteCode = storageService.getUserInfo().siteCode;
				ctrl.report.reportCode = ctrl.report.siteCode + ":" + ctrl.report.serviceCode + ":" + ctrl.report.reportName;
			} else {
				if ( ctrl.report){
					ctrl.report.reportCode = null;
				}
			}
		};
		
		ctrl.companyCodeChanged = function(row, form, isFirst) {
			if(isFirst){
				return;
			}
			if ( !ctrl.report.companyCode ) {
				ctrl.report.companyCode = '*';
				ctrl.report.companyName = '*';
			}
			
			ctrl.report.companyRpts = [];
			if(ctrl.report.companyCode != "ALL" && ctrl.report.companyCode != "*"){
				ctrl.companyGridOptions.enableOperation = false;
				ctrl.companyGridOptions.enableOperationAdd = false;
				ctrl.companyGridOptions.enableRowHeaderSelection = false;
				ctrl.companyGridOptions.enableOperationRemoveSelected=false;
			}else{
				ctrl.companyGridOptions.enableOperation = true;
				ctrl.companyGridOptions.enableOperationAdd = true;
				ctrl.companyGridOptions.enableRowHeaderSelection = true;
				ctrl.companyGridOptions.enableOperationRemoveSelected = true;
			}
		};
		
		ctrl.changeCompanyTypes = function(row, form, isFirst) {
			if(isFirst){
				return;
			}
			if (ctrl.report.companyTypes == 0) {
				ctrl.report.companyCode = '*';
			}
			ctrl.report.companyRpts = [];
		};
		
		ctrl.changeMode = function(){
			if ( ctrl.report && ctrl.report.reportMode){				
				if(ctrl.report.reportMode == "O"){
					ctrl.report.archiveMethod = "N";
					ctrl.report.reportUrl = "cda.reportView.defaultView";
				}else if(ctrl.report.reportMode == "B"){
					ctrl.report.archiveMethod = "D";
					ctrl.report.reportUrl = "com.pti.fsc.cda.ui.report.ReportRunnerDefault";
				}else if(ctrl.report.reportMode == "I"){
					ctrl.report.archiveMethod = "N";
					ctrl.report.reportUrl = "cda.reportView.defaultView";
				}
				
			} else {
				if ( ctrl.report){
					ctrl.report.reportUrl = "cda.reportView.defaultView";
				}
			}
		};

		ctrl.createSave = function() {
			ctrl.report.reportTemplateDocList = ctrl.report.reportTemplateDocList || [];
			if(ctrl.report.reportTemplateDocList.length > 0){
				ctrl.report.docId = ctrl.report.reportTemplateDocList[0].docId;
			}
			if(angular.equals(ctrl.report.isAdhocReport,"N")){
				delete ctrl.report.reportAd;
			}else{
				ctrl.report.reportAd = ctrl.report.reportAd || {};
			}
			if (ctrl.report.taskId) {
				return authService.createContinueSave(ctrl.report).then(saveSuccessful, saveFailed);
			} else {
				return authService.createSave(ctrl.report).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createSubmit = function() {
			ctrl.report.reportTemplateDocList = ctrl.report.reportTemplateDocList || [];
			if(ctrl.report.reportTemplateDocList.length > 0){
				ctrl.report.docId = ctrl.report.reportTemplateDocList[0].docId;
			}
			if(angular.equals(ctrl.report.isAdhocReport,"N")){
				delete ctrl.report.reportAd;
			}else{
				ctrl.report.reportAd = ctrl.report.reportAd || {};
			}
			if (ctrl.report.taskId) {
				return authService.createContinueSubmit(ctrl.report).then(submitSuccessful, submitFailed);
			} else {
				return authService.createSubmit(ctrl.report).then(submitSuccessful, submitFailed);
			}
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.report.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.report.list',
					'state' : 'cda.report.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.extend(ctrl.report, response.data.model);
			}else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);
